<?php
namespace app\ask\controller;

use think\Controller;

class Privateletter extends Controller
{
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->BusinessModel=model("Business.Business");
        $this->PrivateletterModel=model("Post.Privateletter");
    }

    // 查询私信数据
    public function index()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $keywords=$this->request->param('keywords','','trim');
            $page=$this->request->param('page','','trim');
            $limit=5;
            $start=($page-1)*$limit;
            $business=$this->BusinessModel->find($busid);
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }
            $where=['busid'=>$busid];
            if(!empty($keywords))
            {
                $where['content']=['LIKE',"%$keywords%"];
            }

            $list=$this->PrivateletterModel->with(['send'])->where($where)->order('privateletter.createtime','desc')->limit($start,$limit)->select();
            if(!$list)
            {
                $this->error('暂无更多数据');
                exit;
            }else
            {
                $this->success('返回私信列表',null,$list);
            }

        }
    }

    // 查询私信数据
    public function send()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $keywords=$this->request->param('keywords','','trim');
            $page=$this->request->param('page','','trim');
            $limit=5;
            $start=($page-1)*$limit;
            $business=$this->BusinessModel->find($busid);
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }
            $where=['sendid'=>$busid];
            if(!empty($keywords))
            {
                $where['content']=['LIKE',"%$keywords%"];
            }
            $list=$this->PrivateletterModel->with(['business'])->where($where)->order('privateletter.createtime','desc')->limit($start,$limit)->select();
            if(!$list)
            {
                $this->error('暂无更多数据');
                exit;
            }else
            {
                $this->success('返回私信列表',null,$list);
            }

        }
    }

    // 添加私信
    public function add()
    {
        if($this->request->isPost())
        {
            $params=$this->request->param();
            $business=$this->BusinessModel->find($params['busid']);
            if(!$business)
            {
                $this->error('接收方用户不存在');
                exit;
            }
            $send=$this->BusinessModel->find($params['sendid']);
            if(!$send)
            {
                $this->error('发送方用户不存在');
                exit;
            }
            $result=$this->PrivateletterModel->validate('common/Post/Privateletter')->save($params);
            if($result===FALSE)
            {
                $this->error($this->BusinessModel->getError());
                exit;
            }
            $this->success('发送成功');
            exit;
        }
    }

    public function read()
    {
        if($this->request->isPost())
        {
            $privateid=$this->request->param('privateid',0,'trim');
            // halt($privateid);
            $private=$this->PrivateletterModel->find($privateid);
            if(!$private)
            {
                $this->error('私信不存在');
                exit;
            }
            // 更新数据
            $data=[
                'id'=>$privateid,
                'status'=>'1'
            ];
            $result=$this->PrivateletterModel->isUpdate(true)->save($data);
            if($result===FALSE)
            {
                $this->error('已读失败');
                exit;
            }
            $this->success('已读成功');
            exit;
        }
    }

    public function del()
    {
        if($this->request->isPost())
        {
            $privateid=$this->request->param('privateid',0,'trim');
            // halt($privateid);
            $private=$this->PrivateletterModel->get($privateid);
            if(!$private)
            {
                $this->error('私信不存在');
                exit;
            }
            $result=$private->delete();
            if($result===FALSE)
            {
                $this->error('删除失败');
                exit;
            }
            $this->success('删除成功');
            exit;
        }
    }

   
}